﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevelopersToolbox;

namespace TextTools {
    public partial class DiffControl : UserControl {

        public DiffControl() {
            InitializeComponent();
        }

        private void _textDiff_Click(object sender, EventArgs e) {
            //int[] left = ConvertToIntArray(_left.GetDataAsString());
            //int[] right = ConvertToIntArray(_right.GetDataAsString());
            string left = _left.GetDataAsString();
            string right = _right.GetDataAsString();
            Diff.Item[] report = Diff.DiffText(left, right, false, false, false);
            report = Diff.ConvertLinesToChars(report, left, right);
            _diffView.ViewMode = DiffViewMode.Text;
            _diffView.SetResults(ConvertToIntArray(left), ConvertToIntArray(right), report);
        }

        private int[] ConvertToIntArray(string s) {
            int[] result = new int[s.Length];
            for (int i = 0; i < s.Length; i++) {
                result[i] = s[i];
            }
            return result;
        }

        private void _binaryDiff_Click(object sender, EventArgs e) {
            int[] left = ConvertToIntArray(_left.Data);
            int[] right = ConvertToIntArray(_right.Data);
            Diff.Item[] report = Diff.DiffInt(left, right);
            _diffView.ViewMode = DiffViewMode.Binary;
            _diffView.SetResults(left, right, report);
        }

        private int[] ConvertToIntArray(byte[] data)
        {
            int[] result = new int[data.Length];
            for (int i = 0; i < data.Length; i++) {
                result[i] = data[i];
            }
            return result;
        }
    }
}
